﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NewsSpider.App
{
    public partial class SetFrm : Form
    {
        NewsSpider.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\data.ini");

        public SetFrm()
        {
            InitializeComponent();
        }

        private void SetFrm_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = NewsSpider.Utility.CommonHelper.GetCurrentSoftSkin();
            this.Icon = new Icon(NewsSpider.Utility.ICONHelper.SetFormICON());

            string strSQLiteFile = iniFile.GetString("Connection", "sqlite", "");
            if (string.IsNullOrEmpty(strSQLiteFile))
            {
                txtSQLite.Text = "Data Source=" + System.Windows.Forms.Application.StartupPath + "\\data\\sqlite\\NewsSpider.db";
            }
            else
            {
                txtSQLite.Text = strSQLiteFile;
            }
            string strAccessFile = iniFile.GetString("Connection", "access", "");
            if (string.IsNullOrEmpty(strAccessFile))
            {
                txtAccess.Text = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "\\data\\access\\NewsSpider.mdb";
            }
            else
            {
                txtAccess.Text = strAccessFile;
            }

            txtMssql.Text = iniFile.GetString("Connection", "mssql", "");
            txtMysql.Text = iniFile.GetString("Connection", "mysql", "");
            txtOracle.Text = iniFile.GetString("Connection", "oracle", "");
            string strFlag = iniFile.GetString("Connection", "flag", "");
            if (strFlag == "1")
            {
                radioButton1.Checked = true;
            }
            if (strFlag == "2")
            {
                radioButton2.Checked = true;
            }
            if (strFlag == "3")
            {
                radioButton3.Checked = true;
            }
            if (strFlag == "4")
            {
                radioButton4.Checked = true;
            }
            if (strFlag == "5")
            {
                radioButton5.Checked = true;
            }

            txtTableName.Text = iniFile.GetString("Table", "tablename", "");
            txtArticleID.Text = iniFile.GetString("Table", "id", "");
            txtArticleTitle.Text = iniFile.GetString("Table", "title", "");
            txtArticleContent.Text = iniFile.GetString("Table", "content", "");
            txtArticleAuthor.Text = iniFile.GetString("Table", "author", "");
            txtArticleSource.Text = iniFile.GetString("Table", "source", "");
            txtArticleLink.Text = iniFile.GetString("Table", "link", "");
            txtArticleDate.Text = iniFile.GetString("Table", "date", "");
        }

        private void btnSaveSet_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                iniFile.WriteValue("Connection", "flag", "1");
            }
            if (radioButton2.Checked)
            {
                iniFile.WriteValue("Connection", "flag", "2");
            }
            if (radioButton3.Checked)
            {
                iniFile.WriteValue("Connection", "flag", "3");
            }
            if (radioButton4.Checked)
            {
                iniFile.WriteValue("Connection", "flag", "4");
            }
            if (radioButton5.Checked)
            {
                iniFile.WriteValue("Connection", "flag", "5");
            }

            iniFile.WriteValue("Connection", "sqlite", txtSQLite.Text.Trim());
            iniFile.WriteValue("Connection", "access", txtAccess.Text.Trim());
            iniFile.WriteValue("Connection", "mssql", txtMssql.Text.Trim());
            iniFile.WriteValue("Connection", "mysql", txtMysql.Text.Trim());
            iniFile.WriteValue("Connection", "oracle", txtOracle.Text.Trim());

            NewsSpider.DBSqlHelp.ConnectionData.SaveConnectionSQLiteString(txtSQLite.Text.Trim());
            NewsSpider.DBSqlHelp.ConnectionData.SaveConnectionAccessString(txtAccess.Text.Trim());
            NewsSpider.DBSqlHelp.ConnectionData.SaveConnectionMSSQLString(txtMssql.Text.Trim());
            NewsSpider.DBSqlHelp.ConnectionData.SaveConnectionMySQLString(txtMysql.Text.Trim());
            NewsSpider.DBSqlHelp.ConnectionData.SaveConnectionOracleString(txtOracle.Text.Trim());

            MessageBox.Show("保存成功！");
        }

        private void btnSaveTable_Click(object sender, EventArgs e)
        {
            iniFile.WriteValue("Table", "tablename", txtTableName.Text.Trim());
            iniFile.WriteValue("Table", "id", txtArticleID.Text.Trim());
            iniFile.WriteValue("Table", "title", txtArticleTitle.Text.Trim());
            iniFile.WriteValue("Table", "content", txtArticleContent.Text.Trim());
            iniFile.WriteValue("Table", "author", txtArticleAuthor.Text.Trim());
            iniFile.WriteValue("Table", "source", txtArticleSource.Text.Trim());
            iniFile.WriteValue("Table", "link", txtArticleLink.Text.Trim());
            iniFile.WriteValue("Table", "date", txtArticleDate.Text.Trim());

            MessageBox.Show("保存成功！");
        }

        private void btnReValue_Click(object sender, EventArgs e)
        {
            txtTableName.Text = "Articles";
            txtArticleID.Text = "article_id";
            txtArticleTitle.Text = "article_title";
            txtArticleContent.Text = "article_content";
            txtArticleAuthor.Text = "article_author";
            txtArticleSource.Text = "article_source";
            txtArticleLink.Text = "article_link";
            txtArticleDate.Text = "add_date";

            MessageBox.Show("恢复成功！");
        }

        private void btnSelectSQLite_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "SQLite(*.db)|*.db|所有文件(*.*)|*.*";
            openFileDialog.ShowDialog();
            string strUploadFile = openFileDialog.FileName;
            txtSQLite.Text = strUploadFile;
        }

        private void btnSelectAccess_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog2 = new OpenFileDialog();
            openFileDialog2.Filter = "Access(*.mdb)|*.mdb|Access 2007(*.accdb)|*.accdb|所有文件(*.*)|*.*";
            openFileDialog2.ShowDialog();
            string strUploadFile = openFileDialog2.FileName;
            txtAccess.Text = strUploadFile;
        }

    }
}
